import 'dept.dart';
import 'role.dart';

class UserProfileModel {
	String? createBy;
	String? createTime;
	dynamic updateBy;
	dynamic updateTime;
	String? remark;
	int? userId;
	int? deptId;
	String? userName;
	String? nickName;
	String? userType;
	String? email;
	String? phonenumber;
	String? sex;
	String? avatar;
	String? password;
	String? status;
	String? delFlag;
	String? loginIp;
	DateTime? loginDate;
	Dept? dept;
	List<Role>? roles;
	dynamic roleIds;
	dynamic postIds;
	dynamic roleId;
	String? roleGroup;
	String? postGroup;
	bool? admin;

	UserProfileModel({
		this.createBy, 
		this.createTime, 
		this.updateBy, 
		this.updateTime, 
		this.remark, 
		this.userId, 
		this.deptId, 
		this.userName, 
		this.nickName, 
		this.userType, 
		this.email, 
		this.phonenumber, 
		this.sex, 
		this.avatar, 
		this.password, 
		this.status, 
		this.delFlag, 
		this.loginIp, 
		this.loginDate, 
		this.dept, 
		this.roles, 
		this.roleIds, 
		this.postIds, 
		this.roleId, 
		this.roleGroup, 
		this.postGroup, 
		this.admin, 
	});

	factory UserProfileModel.fromJson(Map<String, dynamic> json) {
		return UserProfileModel(
			createBy: json['createBy'] as String?,
			createTime: json['createTime'] as String?,
			updateBy: json['updateBy'] as dynamic,
			updateTime: json['updateTime'] as dynamic,
			remark: json['remark'] as String?,
			userId: json['userId'] as int?,
			deptId: json['deptId'] as int?,
			userName: json['userName'] as String?,
			nickName: json['nickName'] as String?,
			userType: json['userType'] as String?,
			email: json['email'] as String?,
			phonenumber: json['phonenumber'] as String?,
			sex: json['sex'] as String?,
			avatar: json['avatar'] as String?,
			password: json['password'] as String?,
			status: json['status'] as String?,
			delFlag: json['delFlag'] as String?,
			loginIp: json['loginIp'] as String?,
			loginDate: json['loginDate'] == null
						? null
						: DateTime.parse(json['loginDate'] as String),
			dept: json['dept'] == null
						? null
						: Dept.fromJson(json['dept'] as Map<String, dynamic>),
			roles: (json['roles'] as List<dynamic>?)
						?.map((e) => Role.fromJson(e as Map<String, dynamic>))
						.toList(),
			roleIds: json['roleIds'] as dynamic,
			postIds: json['postIds'] as dynamic,
			roleId: json['roleId'] as dynamic,
			roleGroup: json['roleGroup'] as String?,
			postGroup: json['postGroup'] as String?,
			admin: json['admin'] as bool?,
		);
	}



	Map<String, dynamic> toJson() => {
				'createBy': createBy,
				'createTime': createTime,
				'updateBy': updateBy,
				'updateTime': updateTime,
				'remark': remark,
				'userId': userId,
				'deptId': deptId,
				'userName': userName,
				'nickName': nickName,
				'userType': userType,
				'email': email,
				'phonenumber': phonenumber,
				'sex': sex,
				'avatar': avatar,
				'password': password,
				'status': status,
				'delFlag': delFlag,
				'loginIp': loginIp,
				'loginDate': loginDate?.toIso8601String(),
				'dept': dept?.toJson(),
				'roles': roles?.map((e) => e.toJson()).toList(),
				'roleIds': roleIds,
				'postIds': postIds,
				'roleId': roleId,
				'roleGroup': roleGroup,
				'postGroup': postGroup,
				'admin': admin,
			};

	// ==================== 便利Getter方法 ====================
	
	/// 获取性别文本
	String get sexText {
		switch (sex) {
			case '0':
				return '男';
			case '1':
				return '女';
			case '2':
				return '未知';
			default:
				return '未知';
		}
	}
	
	/// 获取状态文本
	String get statusText {
		switch (status) {
			case '0':
				return '正常';
			case '1':
				return '停用';
			default:
				return '未知';
		}
	}
	
	/// 获取用户显示名称（优先使用昵称，然后用户名）
	String get displayName => nickName ?? userName ?? '';
	
	/// 获取头像URL（兼容旧版本的avatarUrl getter）
	String get avatarUrl => avatar ?? '';
}
